// pages/tiezi/tiezi.js
var utils = require('../../utils/utils.js');
Page({

    /**
     * 页面的初始数据
     */
    data: {
        headImg:"https://www.304cloud.top:4321",
        img:[]
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        console.log(
            options.id
        )
        this.setData({
            article_id:options.id
        })
        let userID = wx.getStorageSync('openid')
        this.setData({
          userID:userID
        })
        this.getArticle();
    },
    getArticle:function(){
      wx.request({
        url: 'https://www.304cloud.top:4321/articledetail',
        method: "GET",
          data: {
          article_id:this.data.article_id,
          user_id:this.data.userID
          },
          header: {
          "Content-Type": "application/json"
          },
          success: (res)=> {
            console.log(res.data.data)
            this.setData({
                pinglunList:res.data.data.data22,
                dianzanList:res.data.data.data11,
                dianzanNum:res.data.data.data11.length,
                tiezi:res.data.data.data33,
                tieziTxt:res.data.data.data33.article_txt.replace(/。/g,'\n'),
                ifdianzan:res.data.data.iflike
            })
            this.data.img.push(this.data.headImg+this.data.tiezi.article_img)
            if(this.data.dianzanNum==0){
              this.setData({
                dianzanTxt:"这篇作品还没有得到过小心心"
              })
            }else{
              this.setData({
                dianzanTxt:"觉得这篇作品很棒！"
              })
            }
          }
      })
    },
    dianzan:function(e){
      wx.request({
        url : "https://www.304cloud.top:4321/like",
        method: "GET",
        data: {
          user_id:this.data.userID,
          article_id:e.currentTarget.dataset.id
        },
        header: {
          "Content-Type": "application/json"
        },
        success: (res)=>{
          console.log(res);
        }
      })
      wx.showToast({
        title: '点赞成功！',
        icon: 'success',
        duration: 1000
      });
      this.getArticle();
    },
  del_article:function(e){
    wx.showModal({
      title: '删除作品',
      content: '确定要删除这个作品？',
      cancelText: '取消',
      confirmText: '确定',
      success: res => {
        if (res.confirm) {
          wx.request({
            url : "https://www.304cloud.top:4321/delarticle",
            method: "POST",
            data: {
                user_id:this.data.userID,
                article_id:this.data.article_id
            },
            header: {
              "Content-Type": "application/json"
            },
            success: (res)=>{
              console.log(res);
              if(res.data!='fail'){
                wx.switchTab({
                    url: '/pages/star/star',
                    success: function (e) {  
                      var page = getCurrentPages().pop();  
                      if (page == undefined || page == null) return;  
                      page.onLoad();  
                    } 
                  })
                  wx.showToast({
                    title: '删除文章成功！',
                    icon: 'success',
                    duration: 2000
                    })
                    this.getArticle()
              }else{
                wx.showToast({
                  title: '你没有权限删除哦！',
                  icon: 'none',
                  duration: 2000
                  })
              }
            }
          })
        }
      }
    })

      },
      del_pinglun:function(e){
        wx.showModal({
          title: '删除评论',
          content: '确定要删除这条评论？',
          cancelText: '取消',
          confirmText: '确定',
          success: res => {
            if (res.confirm) {
              wx.request({
                url : "https://www.304cloud.top:4321/delcomment",
                method: "POST",
                data: {
                    comment_id:e.currentTarget.dataset.id,
                    user_id:this.data.userID,
                    article_id:this.data.article_id
                },
                header: {
                  "Content-Type": "application/json"
                },
                success: (res)=>{
                  console.log(res);
                  if(res.data!='fail'){
                      wx.showToast({
                        title: '删除评论成功！',
                        icon: 'success',
                        duration: 2000
                        })
                      this.getArticle();
                  }else{
                      wx.showToast({
                        title: '你没有权限删除哦！',
                        icon: 'none',
                        duration: 2000
                        })
                  }
                }
              })
            }
          }
        })
        
      },
    getPinglun:function(e){
        console.log(e.detail.value)
        this.setData({
          pinglunValue:e.detail.value
        })
      },
    pinglunSubmit:function(){
        console.log("评论")
        this.setData({
            pinglun_txt:this.data.pinglunValue
        });
        console.log(this.data.pinglun_txt)
        wx.request({
        url : "https://www.304cloud.top:4321/comment",
        method: "POST",
        data: {
            user_id:this.data.userID,
            comment_txt:this.data.pinglun_txt, 
            article_id:this.data.article_id,
            comment_time:utils.formatTime(new Date()),
        },
        header: {
            "Content-Type": "application/json"
        },
        success: function (res) {
          console.log(res);
          wx.showToast({
          title: '评论成功！',
          icon: 'success',
          duration: 2000
          })
        },
        })
        this.getArticle()
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    },
    goToUserDetail:function(e){
        let userId=e.currentTarget.dataset.id;
        console.log(userId)
        if(userId == this.data.userID){
          wx.switchTab({
            url: '/pages/my/my',
          })
        }else{
          wx.navigateTo({
            url: '/pages/user/user?id=' +userId
          })
        }
    },
    ViewImage(e) {
      wx.previewImage({
        urls: this.data.img,
        current: this.data.img[0]
      });
    },
})